const express = require("express");
const router = express.Router();
const Send = require("../../../utils/Send");
const Utils = require("../../../utils");
const { Users, ActivityLogs } = require("../../../models");
const Token = require("../../../utils/Token");


/** 查询账户是否可注册 */
router.get("/isAccountCanSignup", async (req, res, next) => {
  const { account } = req.query;
  try {
    const result = await Users.findOne({
      where: { account: account.toLocaleLowerCase() },
      attributes: ["account"],
    });
    res.send(Send.success(!!result));
  } catch (error) {
    next(error);
  }
});

/** 账户密码注册 */
router.post("/accountSignin", async (req, res, next) => {
  const { account, password } = req.body;
  try {
    if (!account || account.length < 3) {
      return res.send(Send.fail("账号长度不能小于3位"));
    }
    if (!password || password.length < 6) {
      return res.send(Send.fail("密码长度不能小于6位"));
    }
    // 判断账户是否被注册
    const user = await Users.findOne({
      where: { account: account.toLocaleLowerCase() },
      attributes: ["account"],
    });
    if (user && user.account) {
      return res.send(Send.fail("账户已被注册"));
    }
    const salt = Utils.createSalt();
    // 创建用户
    await Users.create({ account: account.toLocaleLowerCase(), salt, password: Utils.hashPassword(password, salt) });
    const info = await Users.findOne({ where: { account: account.toLocaleLowerCase() } });
    res.send(Send.success());
  } catch (error) {
    next(error);
  }
});

/** 账户密码登录 */
router.post("/accountLogin", async (req, res, next) => {
  const { account, password } = req.body;
  try {
    if (!account || !password) {
      return res.send(Send.fail("账号密码不能为空"));
    }
    const user = await Users.findOne({ where: { account: account.toLocaleLowerCase() } });
    if (!user) {
      return res.send(Send.fail("账户不存在"));
    }
    const hashPassword = Utils.hashPassword(password, user.salt);
    if (hashPassword !== user.password) {
      return res.send(Send.fail("密码错误"));
    }
    const token = await Token.saveToken(user);
    res.send(Send.success(token));
  } catch (error) {
    next(error);
  }
});



/** 退出登录 */
router.post("/logout", async (req, res, next) => {
  // 从请求头中获取token
  const { token } = Utils.getBaseParams(req);
  try {
    await Token.delete(token);
    res.send(Send.success());
  } catch (error) {
    next(error);
  }
});

/** 获取用户信息 */
router.get("/getInfo", async (req, res, next) => {
  // 从请求头中获取token
  const { token } = Utils.getBaseParams(req);
  try {
    const result = await Token.getInfoByToken(token);
    res.send(Send.success(result));
  } catch (error) {
    next(error);
  }
});

/** 查询日志 */
router.get("/logs", async (req, res, next) => {
  try {
    const { token } = Utils.getBaseParams(req);
    const user = await Token.getInfoByToken(token);
    const { page = 1, limit = 10 } = req.query;
    const offset = (page - 1) * limit;
    const logs = await ActivityLogs.findAll({ offset, limit, where: { user_id: user.id }, order: [["id", "DESC"]] });
    res.send(Send.success(logs));
  } catch (error) {
    next(error);
  }
});

module.exports = router;
